package mapper;

import entity.UserEntity;
import util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HuangJH
 * @create 2023-11-30-9:29
 */
public class UserMapper {

    /**
     * 数据库添加用户
     * @param userEntity
     * @return
     */
    public boolean addUser(UserEntity userEntity) {
        // 先查询是否存在此用户
        List<UserEntity> userList = getUser(userEntity);
        if(null != userList && userList.size() > 0) {
            System.out.println("注册用户已存在：" + userEntity.getAccount());
            return false;
        }
        //声明JDBC 中需要用到的几个变量
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet set = null;
        int successNum = 0;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "insert into user(account, password, identity) values(?, ?, ?);";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userEntity.getAccount());
            preparedStatement.setString(2, userEntity.getPassword());
            preparedStatement.setInt(3, userEntity.getIdentity());
            successNum = preparedStatement.executeUpdate();
        } catch (Exception e) {
            System.out.println("添加用户异常");
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, preparedStatement, connection);
        }
        if (successNum > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查数据库是否存在此用户
     * @param userEntity
     * @return
     */
    public UserEntity login(UserEntity userEntity) {
        // 先查询是否存在此用户
        List<UserEntity> userList = getUser(userEntity);
        if(null == userList || userList.size() <= 0) {
            System.out.println( userEntity.getAccount()+ "登录失败，请检查用户名及密码");
            return null;
        }
        return userList.get(0);
    }

    /**
     * 数据库查询用户
     * @param userEntity
     * @return
     */
    public List<UserEntity> getUser(UserEntity userEntity) {
        //声明JDBC 中需要用到的几个变量
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet set = null;
        List<UserEntity> userEntities = new ArrayList<>();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select * from user where account = ? and password = ? and identity = ?;";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userEntity.getAccount());
            preparedStatement.setString(2, userEntity.getPassword());
            preparedStatement.setInt(3, userEntity.getIdentity());
            set = preparedStatement.executeQuery();
            while (set.next()) {
                UserEntity userEntity1 = new UserEntity();
                userEntity1.setId(set.getInt("id"));
                userEntity1.setIdentity(set.getInt("identity"));
                userEntity1.setAccount(set.getString("account"));
                userEntity1.setPassword(set.getString("password"));
                userEntities.add(userEntity1);
            }
        } catch (Exception e) {
            System.out.println("查询用户异常");
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null, preparedStatement, connection);
        }
        return userEntities;
    }
}
